﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using MassMailing.WPF.Settings;

namespace MassMailing.WPF
{
    /// <summary>
    /// Interaction logic for MailSettings.xaml
    /// </summary>
    public partial class MailSettingsDialogBox : Window
    {
        #region Private Fields

        private MailSettings _settings;
        private int _smtpSettingsNumber = 0;

        #endregion

        #region Constructors

        /// <summary>
        /// Default Constructor
        /// </summary>
        public MailSettingsDialogBox()
        {
            InitializeComponent();

            _settings = MailSettings.LoadSettings();
            SetupSMTPSettings(_settings.SMTPSettings);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// The event handler for the click event on the btnAddSMTP button.
        /// </summary>
        private void btnAddSMTP_Click(object sender, RoutedEventArgs e)
        {
            AddSMTPSetting();
        }

        /// <summary>
        /// The event handler for the click event on all the btnRemoveSetting buttons.
        /// </summary>
        private void btnRemoveSetting_Click(object sender, RoutedEventArgs e)
        {
            Button btnRemoveSetting = sender as Button;

            if (btnRemoveSetting == null)
            {
                throw new ApplicationException();
            }

            string userControlName = String.Concat("ucSMTPSetting", btnRemoveSetting.Tag);
            string separatorName = String.Concat("separator", btnRemoveSetting.Tag);

            SMTPSettingUserControl ucSMTPSetting = (SMTPSettingUserControl)spnlSMTPSettings.FindName(userControlName);
            Separator separator = (Separator)spnlSMTPSettings.FindName(separatorName);
            spnlSMTPSettings.Children.Remove(ucSMTPSetting);
            spnlSMTPSettings.Children.Remove(btnRemoveSetting);
            spnlSMTPSettings.Children.Remove(separator);
        }

        /// <summary>
        /// The event handler for the click event on the btnSave button.
        /// </summary>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            List<SMTPSetting> newSMTPSettings = new List<SMTPSetting>();

            IEnumerable<SMTPSettingUserControl> smtpSettings = spnlSMTPSettings.Children.OfType<SMTPSettingUserControl>().Where(x => x.ContainsSettings);
            foreach (SMTPSettingUserControl ucSMTPSetting in smtpSettings)
            {
                SMTPSetting smtpSetting = new SMTPSetting();
                smtpSetting.SMTPServer = ucSMTPSetting.SMTPServer;
                smtpSetting.SSLEnabled = ucSMTPSetting.SSLEnabled;
                smtpSetting.Port = ucSMTPSetting.Port;
                smtpSetting.MaximumSentEmails = ucSMTPSetting.MaximumSentEmails;
                smtpSetting.BurstSize = ucSMTPSetting.BurstSize;
                smtpSetting.BurstDelay = ucSMTPSetting.BurstDelay;
                smtpSetting.EmailAddress = ucSMTPSetting.EmailAddress;
                smtpSetting.Password = ucSMTPSetting.Password;

                newSMTPSettings.Add(smtpSetting);
            }

            _settings.SMTPSettings = newSMTPSettings;
            MailSettings.SaveSettings(_settings);
            this.Close();
        }

        #endregion

        #region Methods

        private void AddSMTPSetting()
        {
            AddSMTPSetting(null);
        }

        /// <summary>
        /// Adds an SMTP Setting User Control with settings specified by the setting passed in
        /// or blank if the smtp setting is null.
        /// </summary>
        /// <param name="smtpSetting">
        /// The setting to use as a basis for the new smtp setting user control.
        /// </param>
        private void AddSMTPSetting(SMTPSetting smtpSetting)
        {
            Separator separator = new Separator();
            separator.Name = String.Concat("separator", _smtpSettingsNumber);
            separator.Height = 5;

            spnlSMTPSettings.Children.Add(separator);
            spnlSMTPSettings.RegisterName(separator.Name, separator);

            SMTPSettingUserControl ucSMTPSetting = null;
            if (smtpSetting == null) ucSMTPSetting = new SMTPSettingUserControl();
            else ucSMTPSetting = new SMTPSettingUserControl(smtpSetting);
            ucSMTPSetting.Name = String.Concat("ucSMTPSetting", _smtpSettingsNumber);

            spnlSMTPSettings.Children.Add(ucSMTPSetting);
            spnlSMTPSettings.RegisterName(ucSMTPSetting.Name, ucSMTPSetting);

            Button btnRemoveSetting = new Button();
            btnRemoveSetting.Name = String.Concat("btnRemoveSetting", _smtpSettingsNumber);
            btnRemoveSetting.Content = "Remove Setting";
            btnRemoveSetting.BorderThickness = new Thickness(0, 2.5, 0, 10);
            btnRemoveSetting.Tag = _smtpSettingsNumber;
            btnRemoveSetting.Click += new RoutedEventHandler(btnRemoveSetting_Click);

            spnlSMTPSettings.Children.Add(btnRemoveSetting);
            spnlSMTPSettings.RegisterName(btnRemoveSetting.Name, btnRemoveSetting);

            _smtpSettingsNumber++;
        }

        /// <summary>
        /// Sets up initially the smtp settings for the dialog.
        /// </summary>
        /// <param name="smtpSettings">The smtp settings to set them up with.</param>
        private void SetupSMTPSettings(List<SMTPSetting> smtpSettings)
        {
            SMTPSetting firstSetting = smtpSettings.FirstOrDefault();
            SMTPSettingUserControl ucSMTPSetting = null;
            if (firstSetting == null) ucSMTPSetting = new SMTPSettingUserControl();
            else ucSMTPSetting = new SMTPSettingUserControl(firstSetting);
            ucSMTPSetting.Name = string.Concat("ucSMTPSetting", _smtpSettingsNumber);

            spnlSMTPSettings.Children.Add(ucSMTPSetting);
            spnlSMTPSettings.RegisterName(ucSMTPSetting.Name, ucSMTPSetting);

            _smtpSettingsNumber++;

            IEnumerable<SMTPSetting> remainingSettings = smtpSettings.Skip(1);
            foreach (SMTPSetting smtpSetting in remainingSettings)
            {
                AddSMTPSetting(smtpSetting);
            }
        }

        #endregion
    }
}
